package com.dmscpt.controller;

import com.dmscpt.common.Result;
import com.dmscpt.entity.DmscDb;
import com.dmscpt.service.DmscDbService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysConfig;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 代码生成数据库管理 前端控制器
 * </p>
 * @author hnyufushan
 * @since 2025-04-21
 */
@RestController
@RequestMapping("/dmsc/dbManage")
public class DmscDbController {
    @Resource
    DmscDbService dmscDbService;

    /**
     * 数据库列表查询
     * @param db
     * @return
     */
    @RequestMapping("/list")
    public Result selectList(DmscDb db){
        List<DmscDb> dblist = dmscDbService.selectList(db);
        return Result.successPage("数据库列表查询成功",dblist,dblist.size());
    }

    /**
     * 数据库保存
     * @param db
     * @return
     */
    @RequestMapping("/create")
    public Result saveDb(@RequestBody DmscDb db){
        //先判断数据库链接是否正常
        String dbUrl = db.getDbUrl();
        try {
            Connection conn = DriverManager.getConnection(dbUrl,db.getDbUser(),db.getDbPassword());
        } catch (SQLException e) {
            throw new BaseException("数据库连接信息输入不正确");
        }
        if("gauss".equals(db.getDbType())){
            if(dbUrl.indexOf("currentSchema=")>0){
                db.setSchemaName(dbUrl.split("currentSchema=")[1].split("\\&")[0]);
            }else {
                throw new BaseException("数据库URL格式不正确");
            }
        }
        dmscDbService.save(db);
        return Result.success("数据库添加成功");
    }

    /**
     * 查询数据库详细
     * @param configId
     * @return
     */
    @RequestMapping("/update/{dbId}")
    public Result deleteDb(@PathVariable Long dbId){
        DmscDb dmscDb = dmscDbService.getById(dbId);
        return Result.success("查询数据库详细成功",dmscDb);
    }

    /**
     * 数据库修改
     * @param db
     * @return
     */
    @RequestMapping("/update")
    public Result updateDb(@RequestBody DmscDb db){
        //先判断数据库链接是否正常
        String dbUrl = db.getDbUrl();
        try {
            Connection conn = DriverManager.getConnection(dbUrl,db.getDbUser(),db.getDbPassword());
        } catch (SQLException e) {
            throw new BaseException("数据库连接信息输入不正确");
        }
        if("gauss".equals(db.getDbType())){
            if(dbUrl.indexOf("currentSchema=")>0){
                db.setSchemaName(dbUrl.split("currentSchema=")[1].split("\\&")[0]);
            }else {
                throw new BaseException("数据库URL格式不正确");
            }
        }
        dmscDbService.updateById(db);
        return Result.success("数据库修改成功");
    }

    /**
     * 数据库删除
     * @param db
     * @return
     */
    @RequestMapping("/delete/{dbIds}")
    public Result deleteDb(@PathVariable Long[] dbIds){
        dmscDbService.removeByIds(Arrays.asList(dbIds));
        return Result.success("数据库删除成功");
    }

    /**
     * 数据导出
     * @param db
     * @return
     */
    @RequestMapping("/export")
    public AjaxResult exportDb(DmscDb db){
        List<DmscDb> dblist = dmscDbService.selectList(db);
        ExcelUtil<DmscDb> util = new ExcelUtil(DmscDb.class);
        return util.exportExcel(dblist, "数据库数据");
    }
}

